package com.wenli.youkumenudemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.wenli.youkumenudemo.utils.AnimUtils;

import comit.wenli.youkumenudemo.R;

public class MainActivity extends Activity implements OnClickListener {

    private boolean isDisplayLevel3 = true; // 是否显示三级菜单, 默认为: 显示
    private boolean isDisplayLevel2 = true; // 是否显示二级菜单, 默认为: 显示

    private boolean isDisplayMenu = true; // 是否显示菜单, 默认为: 显示

    private RelativeLayout rlLevel3;
    private RelativeLayout rlLevel2;
    private RelativeLayout rlLevel1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        rlLevel1 = (RelativeLayout) findViewById(R.id.rl_level1);
        rlLevel2 = (RelativeLayout) findViewById(R.id.rl_level2);
        rlLevel3 = (RelativeLayout) findViewById(R.id.rl_level3);

        ImageButton ibHome = (ImageButton) findViewById(R.id.ib_home);
        ImageButton ibMenu = (ImageButton) findViewById(R.id.ib_menu);

        ibHome.setOnClickListener(this);
        ibMenu.setOnClickListener(this);
    }

    /**
     * 当一个按键被按下时触发此方法
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 判断当前按下的键是否是菜单键
        if(keyCode == KeyEvent.KEYCODE_MENU) {
            // 当前是菜单键

            if(AnimUtils.runningAnimationCount != 0) {
                // 当前有动画正在执行, 不执行下面代码的动画
                return true;
            }

            if(isDisplayMenu) {
                // 依次判断菜单是否显示, 如果显示执行隐藏的操作
                long startOffset = 0;
                if(isDisplayLevel3) {
                    AnimUtils.startOutRotateAnimation(rlLevel3, startOffset);
                    startOffset += 200;
                    isDisplayLevel3 = false;
                }
                if(isDisplayLevel2) {
                    AnimUtils.startOutRotateAnimation(rlLevel2, startOffset);
                    startOffset += 200;
                    isDisplayLevel2 = false;
                }
                AnimUtils.startOutRotateAnimation(rlLevel1, startOffset);
                isDisplayMenu = false;
            } else {
                // 所有菜单是隐藏的状态, 应该依次的进入显示的状态

                AnimUtils.startInRotateAnimation(rlLevel1, 0);
                AnimUtils.startInRotateAnimation(rlLevel2, 200);
                AnimUtils.startInRotateAnimation(rlLevel3, 400);

                isDisplayLevel3 = true;
                isDisplayLevel2 = true;
                isDisplayMenu = true;
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_home: // 控制的二级菜单显示或者隐藏
                if(AnimUtils.runningAnimationCount != 0) {
                    // 当前有动画正在执行, 不执行下面代码的动画
                    break;
                }

                if(isDisplayLevel2) { // 当前二级菜单是显示的, 需要把二级菜单隐藏
                    // 动画延迟的时间
                    long startOffset = 0;

                    // 判断三级菜单是否显示, 如果显示, 先把三级菜单隐藏
                    if(isDisplayLevel3) {
                        // 当前三级菜单是显示的, 隐藏掉
                        AnimUtils.startOutRotateAnimation(rlLevel3, startOffset);
                        isDisplayLevel3 = false;
                        startOffset += 200;
                    }

                    AnimUtils.startOutRotateAnimation(rlLevel2, startOffset);
                } else { // 当前二级菜单是隐藏的, 需要把二级菜单显示
                    AnimUtils.startInRotateAnimation(rlLevel2, 0);
                }
                isDisplayLevel2 = !isDisplayLevel2;
                break;
            case R.id.ib_menu: // 控制的三级菜单显示或者隐藏
                if(AnimUtils.runningAnimationCount != 0) {
                    // 当前有动画正在执行, 不执行下面代码的动画
                    break;
                }


                if(isDisplayLevel3) {
                    AnimUtils.startOutRotateAnimation(rlLevel3, 0);
                } else {
                    AnimUtils.startInRotateAnimation(rlLevel3, 0);
                }
                isDisplayLevel3 = !isDisplayLevel3;
                break;
            default:
                break;
        }
    }
}
